package softtech.apps.dangtinraovat;

import softtech.apps.utilities.Utilities;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ActivityRegister extends Activity {
	
	ShareExternalServer appUtil;
	AsyncTask<Void, Void, String> shareRegidTask;
	String regId;
	private Button btRegister;
	private EditText edPassword,edConfirmPassword,edPhoneNumber;
	
	private String TAG = "Register Activity";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		final Context context = this;
		btRegister = (Button) findViewById(R.id.btnRegister);
		edPassword = (EditText) findViewById(R.id.edPassword);
		edConfirmPassword = (EditText) findViewById(R.id.edConfirmPassword);
		edPhoneNumber = (EditText) findViewById(R.id.edPhoneNumber);
		 Intent i = getIntent();
	     regId = i.getStringExtra("regId");
		btRegister.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				// Form check here
				final String password = edPassword.getText().toString().trim();
				String confirm_password = edConfirmPassword.getText().toString().trim();
				final String phone_number = edPhoneNumber.getText().toString().trim();
				
				if(!TextUtils.isEmpty(password) && !TextUtils.isEmpty(confirm_password) && password.equals(confirm_password)){
					appUtil = new ShareExternalServer();
					shareRegidTask = new AsyncTask<Void, Void, String>() {
						@Override
						protected String doInBackground(Void... params) {
							//Log.d(TAG, "########## Bat dau thuc hien dang ky ");
							String pass_md5 = Utilities.md5(password);
							String result = appUtil.shareRegIdWithAppServer(context,regId,phone_number,pass_md5);
							return result;
						}

						@Override
						protected void onPostExecute(String result) {
							shareRegidTask = null;
							Toast.makeText(context, result,
									Toast.LENGTH_LONG).show();
						}

					};
					shareRegidTask.execute(null, null, null);
				}else{
					alertMessage("Something wrong, please check agian !!");
				}
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_register, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
    void alertMessage(String message) {
        AlertDialog.Builder bld = new AlertDialog.Builder(this);
        bld.setMessage(message);
        bld.setNeutralButton("OK", null);
        Log.d(TAG, "Showing alert dialog: " + message);
        bld.create().show();
    }
}
